import 'package:get/get.dart';
import 'package:kkhousekeep/config/constant.dart';
import 'package:kkhousekeep/models/home_village_entity.dart';
import 'package:kkhousekeep/net/home_connect.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';

import '../../models/home_room_entity.dart';

class SwitchHousesController extends GetxController {
  var rooms = <HomeRoomObjList>[].obs;
  var villages = <HomeVillageObj>[].obs;

  var id = (-1).obs;

  UserType userType = UserType.tenant;

  late HomeConnect _connect;

  @override
  void onInit() {
    id.value = Get.arguments[ArgumentKey.id] ?? id.value;
    userType = Get.arguments[ArgumentKey.userType] ?? userType;
    _connect = Get.find<HomeConnect>();
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    if (userType == UserType.property) {
      _connect.getPropertyVillages().then((value) => villages.value = value);
      return;
    }
    _connect.getRooms().then((value) => rooms.value = value);
  }

  void performClickItemTap(int index) {
    if (userType == UserType.property) {
      var model = villages[index];
      id.value = model.id;
      _connect.switchPropertyVillage(id: model.id).then((value) {
        var result = {
          ArgumentKey.token: value,
          ArgumentKey.data: model,
        };
        Get.back(result: result);
      });
    }

    var model = rooms[index];
    id.value = model.roomId;
    _connect.switchHouse(id: model.roomId).then((value) {
      var result = {
        ArgumentKey.token: value,
        ArgumentKey.data: model,
      };
      Get.back(result: result);
    });
  }
}
